setwd('C:/R_clim')
# for the trim funciton
library(raster)
# A test to load raster package if available if not install it.
if(require(raster) == FALSE) 
              {install.packages("raster",repo="http://stat.ethz.ch/CRAN/")}

 


#create class A station and use stringAsFactors to convert carachter to vector
classA<-read.csv("Stations_data_classA.csv", stringsAsFactors=FALSE)
#create class C station 
classC<-read.csv("Stations_data_classC.csv", stringsAsFactors=FALSE)

dim(classA)
dim(classC)

# remove spaces around names
classA1 <- trim(classA)  
classC1 <- trim(classC)

#make all the Capital letter small
classA1$Month <- tolower(classA1$Month)  
classC1$Month <- tolower(classC1$Month)


# now aggregate the Precip data by station and month and Year:
PrecipclassA<- cbind(class='A',aggregate(classA1$Precipitation, classA1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(PrecipclassA)[8] <- 'Precip'

# aggregate the Solar radiation data by station and month and Year:
RadclassA<- cbind(class='A',aggregate(classA1$Solar_Radiation, classA1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(RadclassA)[8] <- 'Radiation'

# aggregate the wind direction  data by station and month and Year:
winddirclassA<- cbind(class='A',aggregate(classA1$Wind_direction, classA1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(winddirclassA)[8] <- 'wind_direction'

# aggregate the wind speed average data by station and month and Year:
windspeedAvclassA<- cbind(class='A',aggregate(classA1$Wind_Speed_average, classA1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(windspeedAvclassA)[8] <- 'wind_Speed_Av'

# aggregate the wind speed maximum data by station and month and Year:
windspeedMaxclassA<- cbind(class='A',aggregate(classA1$Wind_Speed_max, classA1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(windspeedMaxclassA)[8] <- 'wind_Speed_MAx'

# aggregate the leaf wetness data by station and month and Year:
LeafWetclassA<- cbind(class='A',aggregate(classA1$Leaf_Wetness, classA1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(LeafWetclassA)[8] <- 'Leaf_Wetness'

# aggregate the HC air temperature average data by station and month and Year:
AirTempclassA<- cbind(class='A',aggregate(classA1$HC_Relative_humidity, classA1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(AirTempclassA)[8] <- 'Air_Temperature'

# aggregate the HC relative humidity data by station and month and Year:
HRclassA<- cbind(class='A',aggregate(classA1$HC_Relative_humidity, classA1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(HRclassA)[8] <- 'Relative_humidity'

# aggregate the Dew point average data by station and month and Year:
DewAvclassA<- cbind(class='A',aggregate(classA1$Dew_point_Av, classA1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(DewAvclassA)[8] <- 'Dew_point_Av'

# aggregate the Dew point average data by station and month and Year:
DewMinclassA<- cbind(class='A',aggregate(classA1$Dew_point_Min, classA1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(DewMinclassA)[8] <- 'Dew_point_Min'

# aggregate the Water mark data by station and month and Year:
WaterMark1classA<- cbind(class='A',aggregate(classA1$water_mark1, classA1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(WaterMark1classA)[8] <- 'water_Mark1'
WaterMark2classA<- cbind(class='A',aggregate(classA1$water_mark2, classA1[, c('Station', 'Month','Year')], mean, na.rm=TRUE))
colnames(WaterMark2classA)[5] <- 'water_Mark2'
WaterMark3classA<- cbind(class='A',aggregate(classA1$water_mark3, classA1[, c('Station', 'Month','Year')], mean, na.rm=TRUE))
colnames(WaterMark3classA)[5] <- 'water_Mark3'

WMclassA<-cbind(WaterMark1classA,WaterMark2classA[5],WaterMark3classA[5])
Soil_moisture<-apply(WMclassA[,8:10],1,mean)
SoilmoistClassA<-cbind(WMclassA[1:7],Soil_moisture)


# aggregate the Soil temperature 10 cm data by station and month and Year:
SoilTemp10classA<- cbind(class='A',aggregate(classA1$Soil_temp10cm_Av, classA1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(SoilTemp10classA)[8] <- 'Soil_temp_10cm'

# aggregate the Soil temperature 20cm data by station and month and Year:
SoilTemp20classA<- cbind(class='A',aggregate(classA1$Soil_temp20cm_Av, classA1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(SoilTemp20classA)[8] <- 'Soil_temp_20cm'

# aggregate the Air pressure Average data by station and month and Year:
AirPressclassA<- cbind(class='A',aggregate(classA1$Air_pressure_Av, classA1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(AirPressclassA)[8] <- 'Air_pressure'


#group all class A
ClassATotal<-cbind(PrecipclassA,LeafWetclassA[8],AirTempclassA[8],HRclassA[8],DewAvclassA[8],DewMinclassA[8],RadclassA[8],winddirclassA[8],
                   windspeedAvclassA[8],windspeedMaxclassA[8],SoilmoistClassA[8],SoilTemp10classA[8],SoilTemp20classA[8],AirPressclassA[8])

head(ClassATotal)

#Class C station Data
# now aggregate the Precip data by station and month and Year class C:
PrecipclassC<- cbind(class='C',aggregate(classC1$Precipitation, classC1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(PrecipclassC)[8] <- 'Precip'

# now aggregate the Leaf wetness data by station and month and Year class C:
LeafWetclassC<- cbind(class='C',aggregate(classC1$Leaf_Wetness, classC1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(LeafWetclassC)[8] <- 'Leaf_wetness'

# now aggregate the Air temperature data by station and month and Year class C:
AirTempclassC<- cbind(class='C',aggregate(classC1$HC_Air_temp_Av, classC1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(AirTempclassC)[8] <- 'Air_Temperature'

# now aggregate the relative humidity data by station and month and Year class C:
HRclassC<- cbind(class='C',aggregate(classC1$HC_Relative_humidity, classC1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(HRclassC)[8] <- 'Relative_humidity'

# now aggregate the dew point data by station and month and Year class C:
DewAvclassC<- cbind(class='C',aggregate(classC1$Dew_point_Av, classC1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(DewAvclassC)[8] <- 'Dew_point_Av'

# now aggregate the dew point data by station and month and Year class C:
DewMinclassC<- cbind(class='C',aggregate(classC1$Dew_point_Min, classC1[, c('Station', 'Month','Year','X','Y','Z')], mean, na.rm=TRUE))
colnames(DewMinclassC)[8] <- 'Dew_point_Min'

#group all class C
ClassCTotal<-cbind(PrecipclassC,LeafWetclassC[8],AirTempclassC[8],HRclassC[8],DewAvclassC[8],DewMinclassC[8])

head(ClassCTotal)

#join the rows
StationsAC<-merge(ClassATotal,ClassCTotal,all=TRUE)